﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RpcGenerator
{
    class InterfaceGenerator
    {
        public static void Generate(Type type, IMessageSerializer serializer, List<MessageDescription> allDescriptions)
        {
            serializer.NewLine(string.Format("public interface {0}Asyn",type.Name));
            serializer.BeginBlock();
            {
                List<MessageDescription> allRequest = allDescriptions.Where(desc => desc.IsRequest && desc.AsynMethod && desc.Response!= null).ToList();
                allRequest.ForEach(description => 
                {
                    GenerateMethod(description, serializer);
                });
            }
            serializer.EndBlock();
        }

        private static void GenerateMethod(MessageDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine(string.Format("void {0}(",description.Method.Method.Name));
            if (description.Fields.Count > 0)
            {
                description.Fields.ForEach(fieldDescription => 
                {
                    serializer.Apppend(string.Format("{0} {1},", fieldDescription.IsGenericType() ? fieldDescription.GenericTypeName : fieldDescription.Type.Name, fieldDescription.Name));
                });
            }
            string returnCallback = description.Response.Fields[0].IsGenericType() ? description.Response.Fields[0].GenericTypeName : description.Response.Fields[0].Type.Name;
            serializer.Apppend(string.Format("Action<{0}> callback", returnCallback));
            serializer.Apppend(");");
        }
    }
}
